package com.ilop.sthome.common.command;

import android.content.Context;
import android.util.Log;

import com.aliyun.alink.linksdk.tmp.device.panel.PanelDevice;
import com.ilop.sthome.common.udp.GatewayUdpUtil;
import com.ilop.sthome.common.udp.UdpCommunicationUtil;
import com.ilop.sthome.data.greenDao.DeviceInfoBean;

/**
 * @author skygge
 * @Date on 2020-02-25.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：发送自动化相关指令
 */
public class SendAutoCommand {

    private static final String TAG = SendAutoCommand.class.getSimpleName();

    private Context mContext;
    private String mIotId;
    private SiterCommand mSend;
    private String mDeviceName;

    public SendAutoCommand(Context context, DeviceInfoBean device) {
        this.mContext = context;
        this.mIotId = device.getIotId();
        this.mDeviceName = device.getDeviceName();
        mSend = new SiterCommand(device);
    }

    /**
     * 发送json服务
     * @param groupCode json
     */
    private void sendAction(String groupCode){
        if (GatewayUdpUtil.getInstance().isGatewayIntranet(mDeviceName)){
            Log.i(TAG, "sendCodeUDP: " + groupCode);
            new UdpCommunicationUtil(mDeviceName, groupCode);
        }else {
            Log.i(TAG, "sendCodeTCP: " + groupCode);
            PanelDevice panelDevice = new PanelDevice(mIotId);
            panelDevice.init(mContext, null);
            panelDevice.invokeService(groupCode, (b, o) -> {});
        }
    }

    /**
     * 同步自动化
     * @param code 自动化生效位
     * @param page 生效自动化CRC校验码 page1
     * @return
     */
    public void synAutomation(String code, String page){
        sendAction(mSend.synAutomation(code, page));
    }

    /**
     * 同步自动化
     * @param code 自动化生效位
     * @param page 生效自动化CRC校验码 page1
     * @param page2 生效自动化CRC校验码 page2
     * @return
     */
    public void synAutomationWithPage2(String code, String page, String page2){
        sendAction(mSend.synAutomationWithPage(code, page, page2));
    }

    /**
     * 添加自动化
     * @param autoCode 自动化信息
     */
    public void increaseAutomation(String autoCode){
        sendAction(mSend.increaseAutomation(autoCode));
    }

    /**
     * 修改自动化
     * @param autoCode 自动化信息
     */
    public void updateAutomation(String autoCode){
        sendAction(mSend.updateAutomation(autoCode));
    }

    /**
     * 删除自动化
     * @param mid 自动化Id
     */
    public void deleteAutomation(int mid){
        sendAction(mSend.deleteAutomation(mid));
    }

    /**
     * APP点击执行
     * @param mid 自动化Id
     */
    public void autoClick(int mid){
        sendAction(mSend.autoClick(mid));
    }
}
